/**
 * AdminUsers State
 *
 * @date 2020-07-12
 * @author 逗号米
 */

import Vue from 'vue'
import { GetterTree, MutationTree, ActionTree, Module, Commit } from 'vuex'

import * as http from '@/http/cms/admin/admin-user'
import { isArray, arrayFindIndex } from '@/utils'

// 接口
export interface State {
  currentPage: number,
  perPage: number,
  total: number,
  adminUsers: Array<any>,
  adminUser: any
}

// 状态
const state: State = {
  currentPage: 1,
  perPage: 20,
  total: 0,
  adminUsers: [],
  adminUser: {}
}

const getters: GetterTree<State, any> = {

}

const mutations: MutationTree<State> = {
  /**
   * 分页
   *
   * @param state
   * @param current_page
   * @param page_size
   * @param total
   */
  page(state: State, {
    data_list,
    current_page,
    per_page,
    total
  }: { data_list: any, current_page: number, per_page: number, total: number }): void {
    state.adminUsers = data_list
    state.currentPage = current_page
    state.perPage = per_page
    state.total = total
  },
  
  /**
   * 添加
   *
   * @param state
   * @param data
   */
  store(state: State, data: any): void {
    if (state.adminUsers.length === 0) {
      state.adminUsers = [data]
    } else {
      state.adminUsers.push(data)
    }
  },
  
  /**
   * 显示
   *
   * @param state
   * @param data
   */
  show(state: State, data: any) {
    state.adminUser = data
  },
  
  /**
   * 更新
   *
   * @param state
   * @param index
   * @param data
   */
  update(state: State, {index, data}: { index: number, data: any }) {
    Vue.set(state.adminUsers, index, data)
  },
  
  /**
   * 删除
   *
   * @param state
   * @param index
   */
  destroy(state: State, index: number) {
    //Vue.set(state.adminUsers, index, data)
    state.adminUsers.splice(index, 1)
  },
  
  /**
   * 锁定
   *
   * @param state
   * @param data
   */
  setLock(state: State, data: Array<any>) {
    if (isArray(data)) {
      data.forEach((item: any) => {
        const index = arrayFindIndex(state.adminUsers, 'id', item.id)
        Vue.set(state.adminUsers, index, item)
      })
    }
  },
  
  avatar(state: State, data: any) {
  
  }
}

const actions: ActionTree<State, any> = {
  /**
   * 首页
   *
   * @param commit
   * @param payload
   * @returns {Promise<any>}
   */
  index({commit}: { commit: Commit }, payload: any): Promise<any> {
    return  http.index(payload.param).then((res) => {
      if (res.status === 200) {
        commit('page', res.data)
      }
      return res
    })
  },
  
  /**
   * 查询
   *
   * @param commit
   * @param payload
   * @return {Promise<any>}
   */
  query({commit}: { commit: Commit }, payload: any): Promise<any> {
    return http.query(payload.param).then((res) => {
      if (res.status === 200) {
        commit('page', res.data)
      }
      return res
    })
    
  },
  
  /**
   * 存储
   *
   * @param commit
   * @param payload
   * @returns {Promise<any>}
   */
  store({commit}: { commit: Commit }, payload: any): Promise<any> {
    return http.store(payload.param).then((res) => {
      if (res.status === 200) {
        commit('store', res.data)
      }
      return res
    })
    
  },
  
  /**
   * 显示
   *
   * @param commit
   * @param payload
   * @return {Promise<any>}
   */
  show({commit}: { commit: Commit }, payload: any): Promise<any> {
    return http.show(payload.id, {}).then((res) => {
      if (res.status === 200) {
        commit('show', res.data)
      }
      return res
    })
  },
  
  /**
   * 更新
   *
   * @param commit
   * @param payload
   * @returns {Promise<any>}
   */
  update({commit}: { commit: Commit }, payload: any): Promise<any> {
    return http.update(payload.id, payload.param).then((res) => {
      if (res.status === 200 && res.data) {
        commit('update', {
          index: payload.index,
          data: res.data
        })
      }
      return res
    })
  },
  
  /**
   * 删除
   *
   * @param commit
   * @param payload
   * @returns {Promise<any>}
   */
  destroy({commit}: { commit: Commit }, payload: any): Promise<any> {
    return http.destroy(payload.id, {}).then((res) => {
      if (res.status === 200) {
        commit('destroy', payload.index)
      }
      return res
    })
  },
  
  /**
   * 恢复删除
   *
   * @param commit
   * @param payload
   * @returns {Promise<any>}
   */
  undelete({commit}: { commit: Commit }, payload: any): Promise<any> {
    return  http.undelete(payload.id, {}).then((res) => {
      if (res.status === 200) {
        commit('update', {
          index: payload.index,
          data: res.data
        })
      }
      return res
    })
    
  },
  
  /**
   * 设置锁定
   *
   * @param commit
   * @param payload
   * @return {Promise<any>}
   */
  setLock({commit}: { commit: Commit }, payload: any): Promise<any> {
    return  http.setLock(payload.status, payload.param).then((res) => {
      if (res.status === 200) {
        commit('setLock', res.data)
      }
      return res
    })
  },
  
  /**
   * 设置头像
   *
   * @param commit
   * @param payload
   */
  setAvatar({commit}: {commit: Commit}, payload: any): Promise<any> {
    return http.avatar(payload.params).then((res) => {
      if (res.status === 200) {
        commit('cms/setUserInfo', res.data, {root: true})
      }
      
      return res
    })
  },
  
  /**
   * 设置
   *
   * @param commit
   * @param payload
   */
  setting({commit}: {commit: Commit}, payload: any): Promise<any> {
    return http.setting(payload.params).then((res) => {
      return res
    })
  }
}

export const adminUser: Module<State, any> = {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}

export default {
  adminUser
}
